<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected function _initAutoloader()
	{
		$autoloader = Zend_Loader_Autoloader::getInstance();
		$autoloader->registerNamespace('Bet');
		$autoloader->pushAutoloader('__autoload');
	}

	protected function _initConfig()
	{
		$configs = $this->getApplication()->getOptions();
		Zend_Registry::set('config', $configs);
	}

	protected function _initAcl() {
		$aclDataXml = new Bet_Util_Xml(APPLICATION_PATH . "/configs/acl.xml"); 
		$acl = new Bet_Acl();

		$groupTags = $aclDataXml->getXDoc()->getElementsByTagName("group");
		foreach ($groupTags as $groupNode) {
			$groupName = $groupNode->getAttribute('name');
			$acl->addRole(new Zend_Acl_Role($groupName));
			foreach ($groupNode->getElementsByTagName('modules') as $modulesNode) {
				foreach ($modulesNode->getElementsByTagName('module') as $moduleNode) {
					$moduleName = $moduleNode->getAttribute('name');
					foreach ($modulesNode->getElementsByTagName('controller') as $controllerNode) {
						$controllerName = $controllerNode->getAttribute('name');
						foreach ($controllerNode->getElementsByTagName('actions') as $actionsNode) {
							foreach ($controllerNode->getElementsByTagName('action') as $actionNode) {
								$actionName = $actionNode->getAttribute('name');
								$resource = new Zend_Acl_Resource($moduleName . Bet_Constants::RESOURCE_SEPARATOR 
										. $controllerName);
								if (!$acl->has($resource)) {
									$acl->addResource($resource);
								}
								$acl->allow($groupName, $resource, $actionName);
							}
						}
					}
				}
			}
		}

		// Register ACL
		Zend_Registry::set('acl', $acl);

        $aclHelper = new Bet_Controller_Action_Helper_Acl(null, array('acl'=>$acl));
        Zend_Controller_Action_HelperBroker::addHelper($aclHelper);
	}

	/**
	 * Init logger for logging all action need to be logged
	 * Log file will be stored in the log_dir as configuration
	 * Log directory will be set as rotated storing
	 * In the first, we will log into file and FirePHP
	 */
	protected function _initLogger()
	{
		// Get the configuration
		$configs = $this->getApplication()->getOptions();
		$logDir = $configs["log"]["log_dir"];
		$errorLogFile = $configs['log']['file']['error'];
		// Log for all error/warning/notice
		ini_set('error_reporting', E_ALL);
		ini_set('log_errors', '1');
		ini_set('error_log', $logDir . $errorLogFile);

		Bet_Log::setConfigs($configs);
		$betLogger = Bet_Util::getBetLogger();

		// Register loggers
		Zend_Registry::set('logger', $betLogger);

	}

}

